package com.apes.scm.account.transit;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.jpa.tuple.date.DateTime;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.masterdata.stock.model.Location;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;

/**
 * 功能：未达账项
 *
 * @author xul
 * @create 2020-02-12 18:41
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = DepositInTransit.CONST_TABLE_NAME, indexes = {
        @Index(columnList = "ORDER_NUMBER,SOURCE_BUSINESS_TYPE,ORDER_SERIAL_NUMBER,LOCATION_ID", unique = true),
        @Index(columnList = "PRODUCT_ID,LOCATION_ID")})
public class DepositInTransit extends Model {
    public static final String CONST_TABLE_NAME = "acc_deposit_in_transit";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "未达账项")
    @Comment("内码")
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "COMPANY_ID", referencedColumnName = "id", nullable = false)
    @Comment("公司编码")
    private PartyRole company;

    @Column(name = "SOURCE_BUSINESS_TYPE", length = 32)
    @Comment("来源单据类型")
    private String sourceBusinessType;

    @Column(name = "ORDER_NUMBER", nullable = false, length = 32)
    @Comment("单据号码")
    private String orderNumber;

    @Column(name = "ORDER_SERIAL_NUMBER", length = 30, nullable = false)
    @Comment("单据序号")
    private String orderSerialNumber;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "LOCATION_ID", referencedColumnName = "id", nullable = false)
    @Comment("仓库库区")
    private Location location;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "PRODUCT_ID", referencedColumnName = "id", nullable = false)
    @Comment("商品编码")
    private Product product;

    @Column(name = "OCCUR_QTY", nullable = false, columnDefinition = "NUMBER(16,4) DEFAULT 0")
    @Comment("业务发生数")
    private double occurQty = 0.0;

    @Column(name = "CUSTODY_QTY", nullable = false, columnDefinition = "NUMBER(16,4) DEFAULT 0")
    @Comment("保管数")
    private double custodyQty = 0.0;

    @Column(name = "USABLE_QTY", nullable = false, columnDefinition = "NUMBER(16,4) DEFAULT 0")
    @Comment("可用数")
    private double usableQty = 0.0;

    @Column(name = "next_operation", length = 120)
    @Comment("下一步操作")
    private String nextOperation;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "receiving_dept_id", referencedColumnName = "id")
    @Comment("对方部门")
    private Dept receivingDept;

    /*
     * 功能：更新保管数
     */
    public void addCustodyQty(double qty) {
        this.custodyQty += qty;
    }

    /*
     * 功能：更新可用数
     */
    public void addUsableQty(double qty) {
        this.usableQty += qty;
    }
}
